package org.javaboy.web.controller.ai_interview;

import org.javaboy.ai.domain.CategoryItem;
import org.javaboy.ai.service.ICategoryItemService;
import org.javaboy.ai.service.ICategoryService;
import org.javaboy.common.annotation.Log;
import org.javaboy.common.core.controller.BaseController;
import org.javaboy.common.core.domain.AjaxResult;
import org.javaboy.common.core.page.TableDataInfo;
import org.javaboy.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/categoryItem")
public class CategoryItemController extends BaseController {

    @Autowired
    ICategoryItemService categoryItemService;
    @Autowired
    ICategoryService categoryService;

    @GetMapping("/getAllCategoryName")
    public List<String> getAllCategoryName() {
        return categoryService.getAllCategoryName();
    }

    @PreAuthorize("@ss.hasPermi('ai:categoryItem:search')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryItem categoryItem) {
        startPage();
        List<CategoryItem> list = categoryItemService.selectCategoryItemList(categoryItem);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('ai:categoryItem:add')")
    @Log(title = "科目细则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryItem categoryItem) {
        if (categoryItemService.checkValueAndTextExists(categoryItem)) {
            return error("新增'" + categoryItem.getValue() + "'失败，value 或者 text 已存在");
        }
        return toAjax(categoryItemService.save(categoryItem));
    }

    @PreAuthorize("@ss.hasPermi('ai:categoryItem:remove')")
    @Log(title = "科目细则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryItemIds}")
    public AjaxResult remove(@PathVariable Long[] categoryItemIds) {
        return toAjax(categoryItemService.deleteCategoryItems(categoryItemIds));
    }

    @PreAuthorize("@ss.hasPermi('ai:categoryItem:update')")
    @Log(title = "科目细则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryItem categoryItem) {
        if (categoryItemService.checkValueAndTextExists(categoryItem)) {
            return error("新增'" + categoryItem.getValue() + "'失败，value 或者 text 已存在");
        }
        return toAjax(categoryItemService.updateById(categoryItem));
    }

    @PreAuthorize("@ss.hasPermi('ai:categoryItem:search')")
    @GetMapping(value = "/{categoryItemId}")
    public AjaxResult getInfo(@PathVariable Integer categoryItemId) {
        return success(categoryItemService.getById(categoryItemId));
    }
}
